HeaderList* new_HeaderList()
{
	HeaderList *self = (self*) calloc(1, sizeof(HeaderList)); 
	
	self->head = NULL;
	self->tail = NULL;
	
	return *self; 

}

void delete_HeaderList(HeaderList *list)
{
	
	HeaderListNode *nxt_iter = NULL;
	HeaderListNode *iter = list->head;
   
	while (iter != NULL)
	{
		nxt_iter = iter->next; 
		free (iter->name);
      free(iter->value);
      free(iter);
		iter = nxt_iter;
	} 
free(list);
}

void HeaderList_addHeader(HeaderList *list, char *name, char *value)
{
	// Allocate memory for the node
	HeaderListNode *ptr2HeaderNode = (HeaderListNode*) malloc(sizeof(HeaderListNode)); 
	
	// Store the value for name
	ptr2HeaderNode->name = strdup(name);

	// Store the value for value
	ptr2HeaderNode->value = strdup(value); 
	
	// Set the pointer to the next node to NULL
	ptr2HeaderNode->next = NULL; 
	
	// Set the current tail's next pointer, to point to the new node. 
	list->tail->next = *ptr2HeaderNode; 
	
	// Set the tail of the list to the new node. 
	list->tail = *ptr2HeaderNode; 

}

void HeaderList_addToLastHeader(HeaderList *list, char *value)
{
  if(!list->tail) return;
  char *tmpbuf = calloc(strlen(value) + strlen(list->tail->value) + 2, sizeof(char));
  if(!tmpbuf) return;
  
  //yes, strcat is ok as we did strlens in the above calloc
  strcat(tmpbuf, list->tail->value);
  strcat(tmpbuf, " "); //do we add space before continuation or not?
  strcat(tmpbuf, value);
  
  //now change the pointer to point to the new string.
  free(list->tail->value);
  list->tail->value = tmpbuf;
}

